package com.erpanderp.wam.adm.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.adm.model.entity.CustomLookup;

import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;


@ManagedBean
@ViewScoped
public class CreateCustomLookupsBean {
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private List<CustomLookup>  customLookupList = new ArrayList<CustomLookup>();
	private boolean renderNewCustomLookupPanel;
	private CustomLookup customLookup = new CustomLookup();
	private boolean FORM_VALID;
	CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	private boolean rendrerEditCustomLookupPanel;
	private Map<String,Long> customLookupsMap = new HashMap<String, Long>();
	private String stringName;
	
	public CreateCustomLookupsBean(){
		customLookupList = new CustomLookupDAO().getAllCustomLookups();
		
	}
	
	
	
	public void addNewCustomLookupButtonActionListener(ActionEvent e){
		System.out.println("Action Listener...");
		
		
		setRenderNewCustomLookupPanel(true);
	}
	
	
	
	public void saveCustomLookup(ActionEvent e){
		validateForm();
		
		if(FORM_VALID){
			System.out.println("FORM VALID "+FORM_VALID);
			
			customLookup.setCreateDate(new Date());
			customLookup.setCreatedBy(wamUser.getEmployee().getFirstName());
            
			customLookup.setStatus(CommonLookup.STATUS_ACTIVE);
			if(new CustomLookupDAO().isAvailableCustomLookup(customLookup.getCustomLookupName(),customLookup.getDisplayName())){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, customLookup.getCustomLookupName()+" Custom lookup exists.", ""));
			}else{
			if(customLookupDAO.createCustomLookup(customLookup) != null){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,customLookup.getCustomLookupName()+" Custom lookup created successfully.", ""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Failed to custom lookup.", ""));
			}		
			}
			
			
			//Retrieve permissions from database...
			
		}
			
	}
	
	public void discard(ActionEvent e){
		reset();
	}
	
	
	
	public void updateCustomLookup(AjaxBehaviorEvent a){
		
		CustomLookup selectedCustomLookup = (CustomLookup) a.getSource();
		new CustomLookupDAO().updateCustomLookup(selectedCustomLookup);
		
	}
	
	private void reset(){
		//reset fields
		
		customLookup = new CustomLookup();
		setRenderNewCustomLookupPanel(false);
	}
	
	private void validateForm() {
		FORM_VALID = true;

		
		
		if (ValidationUtil.isEmpty(customLookup.getCustomLookupName(), "Custom Lookup ",
				null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		
		if (ValidationUtil.isEmpty(customLookup.getDisplayName(), "Display Name",
				null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
	}


	public List<CustomLookup> getCustomLookupList() {
		return customLookupList;
	}


	public void setCustomLookupList(List<CustomLookup> customLookupList) {
		this.customLookupList = customLookupList;
	}



	public boolean isRenderNewCustomLookupPanel() {
		return renderNewCustomLookupPanel;
	}



	public void setRenderNewCustomLookupPanel(boolean renderNewCustomLookupPanel) {
		this.renderNewCustomLookupPanel = renderNewCustomLookupPanel;
	}



	public CustomLookup getCustomLookup() {
		return customLookup;
	}



	public void setCustomLookup(CustomLookup customLookup) {
		this.customLookup = customLookup;
	}



	public HttpSession getSession() {
		return session;
	}



	public void setSession(HttpSession session) {
		this.session = session;
	}



	public WAMUser getWamUser() {
		return wamUser;
	}



	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}



	public boolean isFORM_VALID() {
		return FORM_VALID;
	}



	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}



	public boolean isRendrerEditCustomLookupPanel() {
		return rendrerEditCustomLookupPanel;
	}



	public void setRendrerEditCustomLookupPanel(boolean rendrerEditCustomLookupPanel) {
		this.rendrerEditCustomLookupPanel = rendrerEditCustomLookupPanel;
	}



	public Map<String, Long> getCustomLookupsMap() {
		return customLookupsMap;
	}



	public void setCustomLookupsMap(Map<String, Long> customLookupsMap) {
		this.customLookupsMap = customLookupsMap;
	}



	public String getStringName() {
		return stringName;
	}



	public void setStringName(String stringName) {
		this.stringName = stringName;
	}



}
